//
//  OneViewController.m
//  Rac
//
//  Created by 崔博 on 2018/1/3.
//  Copyright © 2018年 崔博. All rights reserved.
//
typedef int (^ReduceType)(int acc, int next);
#import "OneViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "BBViewModel.h"
int fold (int *array, int count, ReduceType block, int first)
{
    if (count == 0)  return first;
    return fold(array + 1, count - 1, block, block(first, array[0]));
}
@interface OneViewController ()
@property (nonatomic, strong) RACSignal * sing;
@property (nonatomic, strong) BBViewModel * bbViewModel;
@end

@implementation OneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    
}

void rac_playGround()
{
    int array[] = {1, 2,3 ,4, 5, 6,0, -1};
    int max = fold(array, sizeof(array) / sizeof(int), ^int(int acc, int next) {
        return acc > next ? acc : next;
    }, INT_MIN);
    NSLog(@"---%d",max);
}
- (void) demo1
{
    //    Cocoa桥接
    UIView * view;
    UIButton * view1;
    RACSignal * sig = [view rac_signalForSelector:@selector(setFrame:)];
    RACSignal * sig1 = [view1 rac_signalForControlEvents:UIControlEventTouchUpInside];
    RACSignal * sig2 = RACObserve(view, backgroundColor);
    
    // 信号变换
    RACSignal *signal3 = [sig map:^id _Nullable(NSString * value) {
        return [value substringFromIndex:1];
    }];
    
    // 序列转换
    RACSignal * sig4 = [RACSequence new].signal;
    
    [sig subscribeNext:^(id  _Nullable x) {
        
    } error:^(NSError * _Nullable error) {
        
    } completed:^{
        
    }];
    
    RAC(view, backgroundColor) = sig;
    
    // 桥接
    [view rac_liftSelector:@selector(convertRect:toView:) withSignals:sig,sig1];
    [view rac_liftSelector:@selector(convertRect:toView:) withSignalsFromArray:@[sig,sig1]];
    [view rac_liftSelector:@selector(convertRect:toView:) withSignalOfArguments:sig1];
    
    RACSignal * or = [RACSignal return:@"123"];
    
}
int max (int *array, int count);
int max (int *array, int count)
{
    if (count == 1) {
        return array[0];
    }
    int headMax = max(array, count - 1);
    
    return headMax > array[count - 1] ? headMax : array[count - 1];
}

- (void)dealloc{
    NSLog(@"销毁");
}
- (IBAction)dissmiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
